package office.action.person;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import office.action.person.form.LeaveRequestForm;
import office.bo.person.LeaveRequest;
import office.bo.system.Department;
import office.service.person.IPersonService;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import current.security.user.domain.User;

import platform.common.action.BaseAction;

@Namespace("/person")
@ParentPackage("struts-base")
@Controller("leaveAction")
@Scope("prototype")
public class LeaveAction extends BaseAction {
	private static final long serialVersionUID = 1L;
	private String flag;
	private List<LeaveRequestForm> leaveRequestList = new ArrayList<LeaveRequestForm>();
	private LeaveRequestForm form ;
	@Resource
	private IPersonService personalServiceImpl;
	@SuppressWarnings("unchecked")
	@Action(value="saveOrUpdateLeaveRequest")
	public String saveOrUpdateLeaveRequest() throws Exception{
		LeaveRequest leaveRequest = new LeaveRequest();
		try {
			String role = ((Map)session.get("user")).get("roleName").toString();
			BeanUtils.copyProperties(leaveRequest,form);
			Department d = new Department();
			User u = new User();
			d.setId(getUserDepartmentId());
			u.setId(getUserId());
			leaveRequest.setDepartment(d);
			leaveRequest.setUser(u);
			//modify by xlp
			leaveRequest.setEntryRole(role);
			//只有销售人员大于三天的时候才多走一级审批
			if(leaveRequest != null && leaveRequest.getId() != null && leaveRequest.getId().equals("")){
				leaveRequest.setId(null);
			}
			personalServiceImpl.saveLeaveRequest(leaveRequest);
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		this.getAjaxMessagesJson().setMessage(true, "", "编辑成功");
		return RESULT_AJAXJSON;
	}
	
	@Action(value="gotoLeaveRequest",results={@Result(location="/pages/person/workflow/leaveEdit.jsp")})
	public String gotoLeaveRequest(){
		return "success";
	}
	
	
	@Action(value="getLeaveList",results={@Result(location="/pages/person/workflow/leave.jsp")})
	public String getLeaveList() throws Exception{
		LeaveRequestForm objctForm = new LeaveRequestForm();
		String userId = getUserId();
		objctForm.setEntryUserId(userId);
		String roleId = getUserRoleName();
		objctForm.setCheckRole(roleId);
		String deptId = getUserDepartmentId();
		objctForm.setDeptId(deptId);
		objctForm.setCompanyId(getUserCompanyId());
		objctForm.setRoleDFlag(getRoleDFlag());
		leaveRequestList = personalServiceImpl.getListLeaveRequest(objctForm);
		return "success";
	}
	@Action(value="gotoLeaveMenu",results={@Result(location="/pages/person/workflow/leaveXml.jsp")})
	public String gotoLeaveMenu() throws Exception{
		form = personalServiceImpl.getLeaveRequestById(form.getId());
		return "success";
	}
	
	@Action(value="gotoDeleteLeave",results={@Result(location="/pages/person/workflow/leaveDel.jsp")})
	public String gotoDeleteLeave(){
		return "success";
	}
	
	@Action(value="getLeaveById",results={@Result(location="/pages/person/workflow/leaveEdit.jsp")})
	public String getLeaveById(){
		try {
			LeaveRequest leaveRequest = personalServiceImpl.getLeaveById(form.getId());
			BeanUtils.copyProperties(form,leaveRequest);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "success";
	}
	
	@Action(value="deleteLeaveById")
	public String deleteLeaveById(){
		try {
			personalServiceImpl.deleteLeaveById(form.getId());
		} catch (Exception e) {
			e.printStackTrace();
		}
		this.getAjaxMessagesJson().setMessage(true, "", "删除成功");
		return RESULT_AJAXJSON;
	}

	public IPersonService getPersonalServiceImpl() {
		return personalServiceImpl;
	}

	public void setPersonalServiceImpl(IPersonService personalServiceImpl) {
		this.personalServiceImpl = personalServiceImpl;
	}

	public String getFlag() {
		return flag;
	}

	public void setFlag(String flag) {
		this.flag = flag;
	}

	public List<LeaveRequestForm> getLeaveRequestList() {
		return leaveRequestList;
	}

	public void setLeaveRequestList(List<LeaveRequestForm> leaveRequestList) {
		this.leaveRequestList = leaveRequestList;
	}

	public LeaveRequestForm getForm() {
		return form;
	}

	public void setForm(LeaveRequestForm form) {
		this.form = form;
	}
	
}
